<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({ expectedData, actualData } = {}) {
      const colors = ["#5470C6", "#91CC75", "#EE6666"];
      this.chart.setOption({
        title:{
          show: true,
          text: "各城市退服站址数量(20211119 13:00)"
        },
        color: colors,
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
        },
        grid: {
          right: "20%",
        },
        toolbox: {
          feature: {
            dataView: { show: true, readOnly: false },
            restore: { show: true },
            saveAsImage: { show: true },
          },
        },
        legend: {
          data: ["退服站址数量", "普通站址退服数量", "关键站址退服数量"],
        },
        xAxis: [
          {
            type: "category",
            axisTick: {
              alignWithLabel: true,
            },
            axisLabel: {
              interval: 0,
              rotate: 40,
            },
            // prettier-ignore
            data: ['沈阳市',
              '大连市',
              '鞍山市',
              '抚顺市',
              '本溪市',
              '丹东市',
              '锦州市',
              '营口市',
              '阜新市',
              '辽阳市',
              '铁岭市',
              '朝阳市',
              '盘锦市',
              '葫芦岛市'],
          },
        ],
        yAxis: [
          {
            type: "value",
            name: "退服站址数量",
            min: 100,
            max: 1500,
            position: "left",
            axisLine: {
              show: true,
              lineStyle: {
                color: colors[0],
              },
            },
            axisLabel: {
              formatter: "{value}",
            },
          },
          {
            type: "value",
            name: "普通站址退服数量",
            min: 0,
            max: 150,
            position: "right",
            offset: 40,
            axisLine: {
              show: true,
              lineStyle: {
                color: colors[1],
              },
            },
            axisLabel: {
              formatter: "{value}",
            },
          },
          {
            type: "value",
            name: "关键站址退服数量",
            min: 0,
            max: 10,
            position: "right",
            offset: 160,
            axisLine: {
              show: true,
              lineStyle: {
                color: colors[2],
              },
            },
            axisLabel: {
              formatter: "{value}",
            },
          },
        ],
        series: [
          {
            name: "退服站址数量",
            type: "bar",
            data: [
              299, 171, 287, 1248, 199, 206, 212, 231, 245, 151, 124, 1220, 305,
              320,
            ],
          },
          {
            name: "普通站址退服数量",
            type: "bar",
            yAxisIndex: 1,
            data: [47, 110, 5, 62, 33, 7, 25, 8, 5, 16, 0, 39, 23, 13],
          },
          {
            name: "关键站址退服数量",
            type: "bar",
            yAxisIndex: 2,
            data: [1, 0, 1, 0, 2, 1, 1, 0, 2, 3, 4, 1, 0, 0],
          },
        ],
      });
    },
  },
};
</script>
